package com.cwt_taxi_xa.protocol.card;

import com.cwt_taxi_xa.util.AppLog;

/**
 * 卡片状态管理
 * @author undi
 * 资料参考
 * https://blog.csdn.net/lmj623565791/article/details/26350617
 */
public class CardManager {

	final String TAG = "CardManager";
	private CardState cardCheckInState; 		//签退状态
	private CardState cardCheckInRemoteState; 	//签到检查状态
	private CardState cardSignOffState; 		//签到状态
	private CardState cardTestState; 		//测试状态
	private CardState currentState = cardCheckInState;



	public CardManager() {
		super();
		cardCheckInState = new CardCheckInState();
		cardSignOffState = new CardSignOffState();
		cardCheckInRemoteState = new CardCheckInRemoteState();
		cardTestState = new CardTestState();
	}

	//启动读取IC卡
	public void changeCheckInState(){
		setState(cardCheckInState);
		AppLog.d(TAG,"changeCheckInState ------------------" + AppLog.generateTag());
	}

	public void changeTestState(){
		setState(cardTestState);
	}

	public void changeCheckInRemoteState(){
		setState(cardCheckInRemoteState);
	}

	public void changeSignOffState(){
		setState(cardSignOffState);
	}

	public void receivePackage(String mid,String result,byte[] buffer){
		currentState.receivePackage(mid, result, buffer);
	}

	private void setState(CardState cardState){
		stop();
		this.currentState = cardState;
		start();
	}

	private void start(){
		//AppLog.d(TAG,"start() ------------------");
		if(currentState != null){
			this.currentState.start();
			//AppLog.d(TAG,"currentState.start() ------------------");
		}
	}

	public void startAndRead(){
		//AppLog.d(TAG,"start() ------------------");
		if(currentState != null){
			this.currentState.startAndRead();
			//AppLog.d(TAG,"currentState.start() ------------------");
		}
	}


	public void stop(){
		if(currentState != null){
			this.currentState.stop();
		}
	}
}
